/*
 * Niiuzu
 * Copyright (C) 2011  Thierry Nowak
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.niiuzu.server.dao.impl.dto.mapping;

import com.googlecode.objectify.Key;
import com.niiuzu.server.dao.impl.dto.object.DtoFeedChannel;
import com.niiuzu.server.dao.impl.dto.object.DtoFeedChannelGroup;
import com.niiuzu.server.dao.impl.util.KeyUtil;
import com.niiuzu.shared.model.FeedChannel;

/**
 * Mapping des classes modélisant les flux.
 */
public class FeedChannelMapper extends Mapper<FeedChannel, DtoFeedChannel> {

	@Override
	public FeedChannel mapDtoToSerializable(DtoFeedChannel dtoObject) {

		if (dtoObject == null) {
			return null;
		}

		KeyUtil keyUtil = getKeyUtil();

		FeedChannel serializableObject = new FeedChannel();

		serializableObject.setUid(keyUtil.getEncodedKeyFor(dtoObject));

		serializableObject.setLink(dtoObject.getLink());
		serializableObject.setName(dtoObject.getName());
		serializableObject.setType(dtoObject.getType());

		String channelGroupUid = keyUtil.convertKeyToString(dtoObject
				.getChannelGroupKey());
		serializableObject.setChannelGroupUid(channelGroupUid);

		return serializableObject;
	}

	@Override
	public DtoFeedChannel mapSerializableToDto(FeedChannel serializableObject) {

		if (serializableObject == null) {
			return null;
		}

		KeyUtil keyUtil = getKeyUtil();

		DtoFeedChannel dtoObject = new DtoFeedChannel();

		String channelUid = serializableObject.getUid();
		Key<DtoFeedChannel> channelKey = keyUtil.convertStringToKey(channelUid,
				DtoFeedChannel.class);
		if (channelKey != null) {
			dtoObject.setId(channelKey.getId());
		}

		dtoObject.setLink(serializableObject.getLink());
		dtoObject.setName(serializableObject.getName());
		dtoObject.setType(serializableObject.getType());

		String channelGroupUid = serializableObject.getChannelGroupUid();
		Key<DtoFeedChannelGroup> channelGroupKey = keyUtil.convertStringToKey(
				channelGroupUid, DtoFeedChannelGroup.class);
		dtoObject.setChannelGroupKey(channelGroupKey);

		return dtoObject;
	}

}
